// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from "axios";
import VueAxios from "vue-axios";
import ElementUI from 'element-ui';
import store from "./store";
import dayjs from "dayjs";

Vue.config.productionTip = false
Vue.use(VueAxios, axios);
Vue.use(ElementUI);
Vue.prototype.$moment = dayjs;

Vue.filter("date", function(value, formatStr = "YYYY-MM-DD") {
  return dayjs(value).format(formatStr);
});

Vue.filter("dateTime", function(value, formatStr = "YYYY-MM-DD HH:mm:ss") {
  return dayjs(value).format(formatStr);
});
router.beforeEach((to, from, next) => {

  if (to.path == "/login") {
    if(!store.state.id) {
      next();
    }
    else{
      next({ path: "/" });
    }
  } else if (!store.state.id) {
    next({ path: "/login" });
  } else {
    next();
  }
});

// 响应拦截器
axios.interceptors.response.use(
  function(response) {
    switch (response.data.code) {
      case 40001:
        Vue.prototype.$message({
          type: "error",
          message: response.data.message
        });
        router.push({ path: "/login" });
        break;
      case 50000:
        Vue.prototype.$message({
          type: "error",
          message: response.data.message
        });
        break;
    }
    return response;
  },
  function(error) {
    return Promise.reject(error);
  }
);
/* eslint-disable no-new */
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");
